package com.etoak.stock.service.impl;

import com.etoak.stock.mapper.SysLogMapper;
import com.etoak.stock.pojo.entity.SysLog;
import com.etoak.stock.service.LogService;
import com.etoak.stock.vo.req.LogPageReqVo;
import com.etoak.stock.vo.resp.PageResult;
import com.etoak.stock.vo.resp.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author by 孙有泽
 * @Date 2024/11/10 15:33
 * @Description
 */
@SuppressWarnings("ALL")
@Service
@Slf4j
public class LogServiceImpl implements LogService {
    @Autowired
    private SysLogMapper sysLogMapper;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public void addLog(SysLog sysLog) {
        threadPoolTaskExecutor.execute(()->{
            int i=sysLogMapper.addLog(sysLog);
            if(i>0){
                log.info("日志插入成功"+sysLog.toString());
            }else{
                log.info("日志插头失败"+sysLog.toString());
            }
        });
    }

    @Override
    public R<PageResult> logPageQuery(LogPageReqVo logPageReqVo) {
        PageHelper.startPage(logPageReqVo.getPageNum(),logPageReqVo.getPageSize());
        List<SysLog> logs= sysLogMapper.logPageQuery(logPageReqVo.getUsername(),logPageReqVo.getOperation(),logPageReqVo.getStartTime(),logPageReqVo.getEndTime());
        PageInfo<SysLog> logsPageInfo = new PageInfo<>(logs);
        PageResult<SysLog> sysLogPageResult = new PageResult<>(logsPageInfo);
        return R.ok(sysLogPageResult);
    }
}
